﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

namespace Zero.Dialogue
{
    public class AltarController : MonoBehaviour
    {
        public Vector3 tipOffset = Vector2.up;
        public PlayableDirector timeline;

        private bool _isCanTalk = false; // 是否可对话

        private Animator _animator;
        private SpriteRenderer _spriteRenderer;
        private Collider2D _collider2D;

        private void Start()
        {
            _animator = GetComponent<Animator>();
            _spriteRenderer = GetComponent<SpriteRenderer>();
            _collider2D = GetComponent<Collider2D>();
        }

        private void Update()
        {
            if (_isCanTalk)
            {
                if (Input.GetKeyDown(KeyCode.E))
                {
                    Debug.Log("激活圣坛动画...");
                    _animator.SetTrigger("Activate");

                    // 提高圣坛亮度
                    var color = _spriteRenderer.color;
                    color.a = 1f;
                    _spriteRenderer.color = color;

                    // 隐藏交互提示
                    DialogueSystem.Instance.HideAltarTip();

                    // 移除触发器
                    Destroy(_collider2D);

                    timeline.stopped += director => Debug.Log("进入下一关...");
                    timeline.Play();
                }
            }
        }

        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.CompareTag("Player"))
            {
                _isCanTalk = true;
                DialogueSystem.Instance.ShowAltarTip(transform.position + tipOffset);
            }
        }

        private void OnTriggerExit2D(Collider2D other)
        {
            if (other.CompareTag("Player"))
            {
                _isCanTalk = false;
                DialogueSystem.Instance.HideAltarTip();
            }
        }
    }
}